#!/bin/bash
LOG_FILE="./menu_operation.log"
# 带时间戳的日志
log() {
    echo "[$(date +%T)] 操作: $1" >> $LOG_FILE
}

while true; do
    echo "1. 备份日志"
    echo "2. 清理缓存"
    echo "3. 查看日志"
    echo "0. 退出"
    read -p "请选择: " choice

    case $choice in
        1)
            log "开始备份"
            tar -czf log_backup_$(date +%F).tar.gz /var/log/* && log "备份成功" || log "备份失败"
            ;;
        2) 
            log "清理缓存"
            sync; echo 3 > /proc/sys/vm/drop_caches && log "缓存已清"
            ;;
        3)
            log "查看最近日志"
            tail -n 20 $LOG_FILE
            ;;
        0)
            log "用户退出"
            exit 0
            ;;
        *)
            log "无效输入: $choice"
            echo "输入错误！"
    esac
done
#功能：使用case实现多分支菜单 ，每个操作都有日志追踪 ，用户交互友好 